// pages/orderDetail/orderDetail.ts

import {
  orderInfo,
  addressInfo,
  personInfo,
  pauseOrder,
  orderUpdate,
} from "../../../http/api"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog:false,
    id: "",
    orderPrice:"",
    orderObj: {
      startDate: '',
      endDate: '',
    },
    orderStatus: [],
    orderTypeArr: [
      "外卖单",
      "包月单",
      "堂食单",
    ],
    addressInfo: {},
    oldInfo: {},
    personId:"",
    addressId:"",

    //0-未配送 1-已送达 2-已核销 3-暂停配送 4-已退款 5-已顺延
  },


  isNumber(value) {
    return !isNaN(parseFloat(value)) && isFinite(value);
  },

  handleShowDialog(){
    this.setData({
      showDialog:true
    })
  },

  handleShowOrderCalendar(e){
    const {index} = e.currentTarget.dataset
    const show = this.data.orderObj.orderDetailList[index].show

    this.setData({
      [`orderObj.orderDetailList[${index}].show`]:!show
    })

  },

  handleSubmitOrderPrice(){
   //订阅消息
   app.shopSubscribeMessageNewOrder()
    if(!this.isNumber(this.data.orderPrice)){
      wx.showToast({
        title: '金额不合法',
        icon:'error'
      })
      this.setData({
        orderPrice:""
      })
      return
    }
    wx.showLoading()
    
    const params = {
      id:this.data.id,
      amountPrice:this.data.orderPrice
    }

    orderUpdate(params).then(oldRes => {
      if (oldRes.data.code === 200) {
        wx.showToast({
          title: '修改成功',
          icon:"success"
        })
       
        this.setData({
          orderPrice:"",
          ['orderObj.amountPrice']:params.amountPrice
        })
      }else{
        wx.showToast({
          title: '修改失败',
          icon:"error"
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })

  },


  handleConfirmOrder(){

    //订阅消息
    app.shopSubscribeMessageNewOrder()

    //获取页面栈
    const pageArr = getCurrentPages()
   
    wx.showLoading()
    
    const params = {
      id:this.data.id,
      status:1
    }

    orderUpdate(params).then(oldRes => {
      if (oldRes.data.code === 200) {
        wx.showModal({
          title: '提示',
          content: '订单已确认',
          showCancel:false,
          complete: (res) => {
            
            if (res.confirm) {
              
                
              // wx.reload();
              //通过页面栈长度判跳转方式
              if(pageArr.length==1){
          
                wx.navigateTo({
                  url: '/pages/shop/order/order',
                })
              }else{
                wx.navigateBack()
              }
              
            }
          }
        })
       
      }else{
        wx.showToast({
          title: '操作失败',
          icon:"error"
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  handleMakePhone() {

    wx.makePhoneCall({
      phoneNumber: '' //仅为示例，并非真实的电话号码
    })


  },

  getOldInfo(oldId) {
    wx.showLoading()
    personInfo(oldId).then(oldRes => {
      if (oldRes.data.code === 200) {
        this.setData({
          oldInfo: {
            ...oldRes.data.data
          }
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  getAddressInfo(addressId) {
    wx.showLoading()
    addressInfo(addressId).then(res => {
      if (res.data.code === 200) {
        this.setData({
          addressInfo: {
            ...res.data.data
          }
        })

      }
    }).finally(() => {
      wx.hideLoading()
    })
  },


 

  getData() {

    const {
      id
    } = this.data
    wx.showLoading()
    orderInfo(id).then(res => {
      if (res.data.code === 200) {
        this.setData({
          orderObj: {
            ...res.data.data
          }
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      orderStatus:app.getOrderStatusArr()
    })

    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getData()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    // this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})